import { PlayerEntity } from '../../domain/entity/PlayerEntity';
import { PlayerAchievementValue } from '../../domain/value/PlayerAchievementValue';

// 玩家数据源接口
export interface PlayerDataSource {
  // 玩家相关方法
  getAllPlayers(): Promise<PlayerEntity[]>;
  getPlayerById(id: string): Promise<PlayerEntity | null>;
  getPlayerByUsername(username: string): Promise<PlayerEntity | null>;
  savePlayer(player: PlayerEntity): Promise<boolean>;
  updatePlayer(player: PlayerEntity): Promise<boolean>;
  deletePlayer(id: string): Promise<boolean>;
  
  // 成就相关方法
  getPlayerAchievements(playerId: string): Promise<PlayerAchievementValue[]>;
  addPlayerAchievement(achievement: PlayerAchievementValue): Promise<boolean>;
  savePlayerAchievements(playerId: string, achievements: PlayerAchievementValue[]): Promise<boolean>;
}

// Mock玩家数据源实现（用于预览）
export class MockPlayerDataSource implements PlayerDataSource {
  private mockPlayers: PlayerEntity[] = [
    new PlayerEntity(
      '1',
      'detective1',
      '福尔摩斯',
      '/icons/avatars/sherlock.png',
      '正式侦探',
      450,
      8,
      92,
      '2024-01-15T08:30:00Z',
      '2024-02-20T14:45:00Z'
    ),
    new PlayerEntity(
      '2',
      'detective2',
      '华生医生',
      '/icons/avatars/watson.png',
      '助理侦探',
      280,
      5,
      88,
      '2024-01-20T09:15:00Z'
    )
  ];
  
  private mockAchievements: PlayerAchievementValue[] = [
    new PlayerAchievementValue(
      'first_case',
      '1',
      '初露锋芒',
      '完成第一个案件的调查',
      '案件侦破',
      '/icons/achievements/first_case.png',
      '2024-01-16T11:20:00Z',
      50
    )
  ];
  
  async getAllPlayers(): Promise<PlayerEntity[]> {
    return this.mockPlayers;
  }
  
  async getPlayerById(id: string): Promise<PlayerEntity | null> {
    return this.mockPlayers.find(p => p.id === id) || null;
  }
  
  async getPlayerByUsername(username: string): Promise<PlayerEntity | null> {
    return this.mockPlayers.find(p => p.username === username) || null;
  }
  
  async savePlayer(player: PlayerEntity): Promise<boolean> {
    this.mockPlayers.push(player);
    console.log('Mock save player:', player);
    return true;
  }
  
  async updatePlayer(player: PlayerEntity): Promise<boolean> {
    const index = this.mockPlayers.findIndex(p => p.id === player.id);
    if (index !== -1) {
      this.mockPlayers[index] = player;
      console.log('Mock update player:', player);
      return true;
    }
    return false;
  }
  
  async deletePlayer(id: string): Promise<boolean> {
    this.mockPlayers = this.mockPlayers.filter(p => p.id !== id);
    console.log('Mock delete player:', id);
    return true;
  }
  
  async getPlayerAchievements(playerId: string): Promise<PlayerAchievementValue[]> {
    return this.mockAchievements.filter(a => a.playerId === playerId);
  }
  
  async addPlayerAchievement(achievement: PlayerAchievementValue): Promise<boolean> {
    this.mockAchievements.push(achievement);
    console.log('Mock add achievement:', achievement);
    return true;
  }
  
  async savePlayerAchievements(playerId: string, achievements: PlayerAchievementValue[]): Promise<boolean> {
    // 移除旧的成就
    this.mockAchievements = this.mockAchievements.filter(a => a.playerId !== playerId);
    // 添加新的成就
    this.mockAchievements.push(...achievements);
    console.log('Mock save achievements for player:', playerId);
    return true;
  }
}
